package com.jxc.action.rkgl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.jxc.action.BaseAction;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.Json;
import com.jxc.model.vo.SessionInfo;
import com.jxc.model.vo.VGoods;
import com.jxc.model.vo.VRepertory;
import com.jxc.model.vo.VUser;
import com.jxc.service.GoodsServiceI;
import com.jxc.service.RepertoryServiceI;
import com.jxc.service.UserServiceI;
import com.jxc.util.ExceptionUtil;
import com.jxc.util.ResourceUtil;
import com.opensymphony.xwork2.ModelDriven;

@Namespace("/rkgl")
@Action(value = "repertoryAction", results = { @Result(name = "repertory", location = "/repertory/repertory.jsp"),
		@Result(name = "repertoryAdd", location = "/repertory/repertoryAdd.jsp"),
		@Result(name = "repertoryEdit", location = "/repertory/repertoryEdit.jsp") })
public class RepertoryAction extends BaseAction implements ModelDriven<VRepertory> {
	private final static Logger logger = Logger.getLogger(RepertoryAction.class);
	
	private VRepertory model = new VRepertory();

	@Autowired
	private RepertoryServiceI repertoryService;
	@Autowired
	private UserServiceI userService;
	@Autowired
	private GoodsServiceI goodsService;
	
//	调出仓库
	public void noAuth_combobox() {
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
		VUser vuser = userService.get(sessionInfo.getUserId());
		List<VRepertory> vrepertories = new ArrayList<VRepertory>();
		if (vuser.getReid() != null) {
			VRepertory vrepertory = new VRepertory();
			vrepertory.setId(vuser.getReid());
			vrepertories = repertoryService.list(vrepertory);
		}else {
			VRepertory vrepertory = new VRepertory();
			vrepertory.setUid(sessionInfo.getUserId());
			vrepertories = repertoryService.list(vrepertory);
		}
		this.writeJson(vrepertories);
	}
//	库存查询的仓库
	public void noAuth_combobox2() {
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
		VUser vuser = userService.get(sessionInfo.getUserId());
		List<VRepertory> vrepertories = new ArrayList<VRepertory>();
		if (vuser.getReid() != null) {
			VRepertory vrepertory = new VRepertory();
			vrepertory.setUid(vuser.getUpid());
			vrepertories = repertoryService.list(vrepertory);
		}else {
			VRepertory vrepertory = new VRepertory();
			vrepertory.setUid(sessionInfo.getUserId());
			vrepertories = repertoryService.list(vrepertory);
		}
		this.writeJson(vrepertories);
	}
	
	/**
	 * 收货分发获取当前经销商所有仓库
	 */
	public void noAuth_comboboxAll() {
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
		VUser vuser = userService.get(sessionInfo.getUserId());
		List<VRepertory> vrepertories = new ArrayList<VRepertory>();
		if (vuser.getReid() != null) {
			VRepertory vrepertory = new VRepertory();
			vrepertory.setUid(vuser.getUpid());
			vrepertories = repertoryService.list(vrepertory);
		}else {
			VRepertory vrepertory = new VRepertory();
			vrepertory.setUid(sessionInfo.getUserId());
			vrepertories = repertoryService.list(vrepertory);
		}
		this.writeJson(vrepertories);
	}
//	调入仓库
	public void noAuth_Incombobox() {
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
		VUser vuser = userService.get(sessionInfo.getUserId());
		List<VRepertory> vrepertories = new ArrayList<VRepertory>();
		if (vuser.getReid() != null) {
			VRepertory vrepertory = new VRepertory();
			vrepertory.setId(vuser.getReid());
			vrepertory.setUid(vuser.getUpid());
			vrepertories = repertoryService.findInConfirm(vrepertory);
		}else {
			VRepertory vrepertory = new VRepertory();
			vrepertory.setUid(sessionInfo.getUserId());
			vrepertories = repertoryService.list(vrepertory);
		}
		this.writeJson(vrepertories);
	}
	
	public void findByIds(){
		if(model.getIds()!=null){
			this.writeJson(repertoryService.findByIds(model.getIds()));
		}
	}
	public void noAuth_usedCombobox() {
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
		VUser vuser = userService.get(sessionInfo.getUserId());
		List<VRepertory> vrepertories = new ArrayList<VRepertory>();
		if (vuser.getReid() != null) {
			VRepertory vrepertory = new VRepertory();
			vrepertory.setId(vuser.getReid());
			vrepertory.setName(vuser.getRename());
			vrepertories.add(vrepertory);
		} else {
			VRepertory vrepertory = new VRepertory();
			vrepertory.setUid(sessionInfo.getUserId());
			vrepertories = repertoryService.list(vrepertory);
		}
		for (Iterator<VRepertory> it = vrepertories.iterator(); it.hasNext();) {
			VGoods vgoods = new VGoods();
			vgoods.setRid(it.next().getId());
			Long num = goodsService.count(vgoods);
			if (num < 1)
				it.remove();
		}
		this.writeJson(vrepertories);
	}

	public String repertory() {
		getSubAuths("RKGL_CKGL");
		return "repertory";
	}

	public void datagrid() {
		// 查询当前用户所属仓库，将仓库添加到查询条件中
		// 如当前用户没有所属仓库，则查询经销商为当前用户的仓库，将所有仓库中的品牌查询出来
		// 如当前用户没有所属仓库，也没有哪个仓库的经销商为当前用户，则返回为空！
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
		VUser vuser = userService.get(sessionInfo.getUserId());
		if (model.getUid() != null) {
			this.writeJson(repertoryService.datagrid(model));
		} else {
			if(vuser.getReid()==null){
				model.setUid(sessionInfo.getUserId());
				DataGrid subDg = repertoryService.datagrid(model);
//				List<VRepertory> rows = new ArrayList<VRepertory>();
//				Long total = 0l;
//				rows.addAll(subDg.getRows());
//				total += subDg.getTotal();
//				DataGrid totalDg = new DataGrid();
//				totalDg.setRows(repertoryService.datagrid(model));
//				totalDg.setTotal(total);
				this.writeJson(subDg);
			}else{
				model.setId(vuser.getReid());
				this.writeJson(repertoryService.datagrid(model));
			}
		}
	}

	public String repertoryAdd() {
		return "repertoryAdd";
	}

	public String repertoryEdit() {
		return "repertoryEdit";
	}

	public void add() {
		Json j = new Json();
		try {
			SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
			Long sl = userService.findLimitRepertory(sessionInfo.getUserId());
			if(sl==1){
				j.setSuccess(false);
				j.setMsg("对不起!您的仓库个数超出限制,请联系管理员处理相关事宜");
			}else{
				model.setUid(sessionInfo.getUserId());
				repertoryService.save(model);
				j.setMsg("添加成功！");
				j.setSuccess(true);
			}
		} catch (Exception e) {
			j.setMsg("添加失败!");
			logger.info(ExceptionUtil.getExceptionMessage(e));
		}
		super.writeJson(j);
	}

	public void edit() {
		Json j = new Json();
		try {
			SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
			model.setUid(sessionInfo.getUserId());
			repertoryService.update(model);
			j.setMsg("添加成功！");
			j.setSuccess(true);
		} catch (Exception e) {
			j.setMsg("添加失败！");
			logger.info(ExceptionUtil.getExceptionMessage(e));
		}
		super.writeJson(j);
	}

	public void delete() {
		Json j = new Json();
		repertoryService.delete(model.getIds());
		j.setSuccess(true);
		j.setMsg("删除成功!");
		super.writeJson(j);
	}

	@Override
	public VRepertory getModel() {
		return model;
	}

}
